/*
 * 
 * 
 * 
 */
package com.hboxs.asl.service.impl;

import com.hboxs.asl.dao.CartItemDao;
import com.hboxs.asl.dao.MemberDao;
import com.hboxs.asl.entity.Cart;
import com.hboxs.asl.entity.CartItem;
import com.hboxs.asl.entity.Product;
import com.hboxs.asl.service.CartItemService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * Service - 购物车项
 */
@Service("cartItemServiceImpl")
public class CartItemServiceImpl extends BaseServiceImpl<CartItem, Long> implements CartItemService {

    @Resource(name = "memberDaoImpl")
    private MemberDao memberDao;

    @Resource(name = "cartItemDaoImpl")
    public void setBaseDao(CartItemDao cartItemDao) {
        super.setBaseDao(cartItemDao);
    }

    @Override
    public void changeSelectedStatus(CartItem cartItem, Boolean isSelected) {
        cartItem.setIsSelected(isSelected);
    }

    @Override
    @Transactional
    public void create(Product product, Integer quantity, Cart cart, BigDecimal dealPrice , Long anchorId) {
        CartItem cartItem = new CartItem();
        cartItem.setIsSelected(true);
        if (dealPrice == null) {
            cartItem.setDealPrice(product.getPromotionPrice() == null ? product.getPrice() : product.getPromotionPrice());
        } else {
            cartItem.setDealPrice(dealPrice);
        }
        if(anchorId!=null){
            cartItem.setAnchor(memberDao.find(anchorId));
        }
        cartItem.setDealPoint(product.getPoint());
        cartItem.setDealmPoint(product.getmPoint());
        cartItem.setQuantity(quantity);
        cartItem.setProduct(product);
        cartItem.setCart(cart);
        save(cartItem);
    }

    @Override
    @Transactional
    public void add(CartItem cartItem, Integer quantity) {
        Product product = cartItem.getProduct();
        cartItem.setDealPrice(product.getPromotionPrice() == null ? product.getPrice() : product.getPromotionPrice());
        cartItem.add(quantity);
        cartItem.setIsSelected(true);
        update(cartItem);
    }

    @Override
    public String validateValues4CartItem(Integer quantity, Product product, Cart cart) {
        if (quantity == null || quantity < 1) {
            return "商品数量必须为正整数";
        }
        if (product == null) {
            return "找不到该商品";
        }
        if (!product.getIsMarketable()) {
            return "该商品已下架";
        }

        if (Cart.MAX_PRODUCT_COUNT != null && cart.getCartItems().size() >= Cart.MAX_PRODUCT_COUNT) {
            return "最多允许添加" + Cart.MAX_PRODUCT_COUNT + "种商品至购物车";
        }
        if (cart.contains(product)) {
            CartItem cartItem = cart.getCartItem(product);
            if (CartItem.MAX_QUANTITY != null && cartItem.getQuantity() + quantity > CartItem.MAX_QUANTITY) {
                return "商品数量不允许超过";
            }
            if (product.getStock() != null && cartItem.getQuantity() + quantity > product.getAvailableStock()) {
                return "该商品库存不足";
            }
        } else {
            if (CartItem.MAX_QUANTITY != null && quantity > CartItem.MAX_QUANTITY) {
                return "商品数量不允许超过" + CartItem.MAX_QUANTITY;
            }
            if (product.getStock() != null && quantity > product.getAvailableStock()) {
                return "该商品库存不足!";
            }
        }
        return null;
    }

}